import axios from 'axios';
import store from '../vuex'
import interceptor from './interceptor'

//ajax配置

axios.defaults.baseURL = process.env.BASEURL
axios.defaults.timeout =  120000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


//发送拦截
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    if (config.uptype != 'file' && config.method == 'post') {  //上传文件
        let param = new URLSearchParams();

        for(let key in config.data){
            param.append(key, config.data[key]);
        }
        config.data = param
    }

    if (config.method == 'get') {
        let arr = []
        for(let key in config.data){
            arr.push(key + '=' + config.data[key]);
        }
        config.url = config.url + '?' + arr.join('&')
    }

    //头部添加字段
    //config.headers.authorization = sessionStorage.getItem('token') || ''
    //config.headers.uId = sessionStorage.getItem('uId') || ''

    return config;
}, function (error) {
    // Do something with request error
    window.console.log(error)
    return Promise.reject(error);
});

//接收拦截
axios.interceptors.response.use(interceptor, function (error) {
    // Do something with response error
    return Promise.reject(error);
});


export default axios
